package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.GroupLinkManBean;
import com.yanchangapp.model.request.QueryLinkManRequest;
import com.yanchangapp.model.response.AllLinkManResponse;
import com.yanchangapp.ui.view.LinkManAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BasePageListResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-12.
 * description: 获取关联员工
 */
public class LinkManAtPresenter extends BasePresenter<LinkManAtView> {

    public LinkManAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryLinkManLists(String keywords){
        HeadApiRetrofit.getInstance().getQueryAllLinkMan(new QueryLinkManRequest(keywords))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<AllLinkManResponse>>() {
                    @Override
                    public void onNext(BaseListResponse<AllLinkManResponse> allLinkManResponseBasePageListResponse) {
                        if (getView() != null){
                            if (allLinkManResponseBasePageListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                                returnList(allLinkManResponseBasePageListResponse.getData());
                            }else {
                                getView().queryGroupLinkMainFailure(allLinkManResponseBasePageListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryGroupLinkMainFailure("网络错误");
                        }
                    }
                });
    }

    private void returnList(List<AllLinkManResponse> list){
        List<GroupLinkManBean> linkManBeans = new ArrayList<>();
        if (list == null || list.size() == 0){
            getView().queryGroupLinkManSuccess(linkManBeans);
        }else {
            for (int i =0 ;i<list.size();i++){
                AllLinkManResponse allLinkManResponse = list.get(i);
                if (allLinkManResponse.getEmployeeList() != null){
                    for (int j=0;j<allLinkManResponse.getEmployeeList().size();j++){
                        GroupLinkManBean linkManBean = allLinkManResponse.getEmployeeList().get(j);
                        linkManBean.setGroup(allLinkManResponse.getDepartmentName());
                        linkManBeans.add(linkManBean);
                    }

                }

            }
            getView().queryGroupLinkManSuccess(linkManBeans);
        }
    }
}
